package com.kuang.tcp;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpClient {

    public static void main(String[] args) {
        OutputStream outputStream=null;
        Socket socket=null;
        try {
            //1.获得服务器地址
            InetAddress serverIP = InetAddress.getByName("127.0.0.1");
            int port=9999;
            //2.创建socket连接
            socket = new Socket(serverIP, port);
            //3.发送消息 IO流
            outputStream = socket.getOutputStream();
            outputStream.write("我是客户端，收到请回复".getBytes());

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
